<?php

namespace App\Contracts;

use App\Models\ProfitSharingAccount;
use App\Models\ProfitSharingList;
use Illuminate\Support\Facades\Log;

class WxProfitSharing
{
    /**
     * 分账配置
     * @var array|null
     */
    private $config = null;

    /**
     * WxProfitSharing constructor.
     * @param array $config
     * $config.appId 特约商户appid,
     * $config.mchId 特约商户号,
     * $config.md5Key 特约商户key,
     * $config.appCertPem 证书路径
     * $config.appKeyPem 证书路径
     */
    public function __construct(array $config=[])
    {
        $this->config = $config;
    }

    /**
     * 申请分账
     *
     * * {
     * "SubAppId": "wx20e0b177ee8f6fb0",
     * "SubMchId": "1271010801",
     * "Channel": "testChannel",
     * "TransId": "4200000387201907307256811131",
     * "ProfitSharingReceivers": [{
     *      "profit_sharing_order_id": "201907301655388695_0028657572",
     *      "Accounts": [{
     *          "Type": "MERCHANT_ID",
     *          "Account": "1534383871",
     *          "Amount": 1,
     *          "Description": "测试服务费1"
     *      }]
     *  }]
     * }
     *
     * @param string $channel
     * @param $transId
     * @param $subAppId
     * @param $subMchId
     * @param $profitSharingReceivers
     * @return mixed
     */
    public function req(string $channel, $transId, $subAppId, $subMchId, $profitSharingReceivers)
    {
        foreach ($profitSharingReceivers as $profitSharingReceiver)
        {
            $profitShareModel = ProfitSharingList::where(
                ['channel'=>$channel,'trans_id'=>$transId,'profit_sharing_order_id'=>$profitSharingReceiver['profit_sharing_order_id']]
            )->first();
            if(empty($profitShareModel)) $profitShareModel = new ProfitSharingList;

            $profitShareModel->channel = $channel;
            $profitShareModel->trans_id = $transId;
            $profitShareModel->sub_appid = $subAppId;
            $profitShareModel->sub_mchid = $subMchId;

            $profitShareModel->profit_sharing_order_id = $profitSharingReceiver['profit_sharing_order_id'];
            $profitShareModel->accounts = json_encode($profitSharingReceiver['accounts'],256|64);
            try {
                $profitShareModel->save();
            }catch (\ErrorException $errorException){
                return ['code'=>1, 'msg'=>$errorException->getMessage()];
            }
        }
        return ['code'=>0, 'msg'=>'success'];
    }

    /**
     * 请求多次分账接口
     *
     * @return array
     * @throws Exception
     * @throws \Exception
     */
    public function multiProfitSharing()
    {
        // 1.获取待分账订单
        $profitShareModel = new ProfitSharingList;
        $profitSharingOrders = $profitShareModel::where(['status'=>1])->get();
        if(empty($profitSharingOrders)) return array('code'=>1, 'msg'=>'没有待分账订单');

        $totalCount = count($profitSharingOrders);
        $successCount = 0;
        $failCount = 0;
        foreach ($profitSharingOrders as $profitSharingOrder)
        {
            $postArr = [
                'appid'=>$this->config['appId'],
                'mch_id'=>$this->config['mchId'],
                'sub_mch_id'=>$profitSharingOrder['sub_mchid'],
                'sub_appid'=>$profitSharingOrder['sub_appid'],
                'nonce_str'=>md5(time() . rand(1000, 9999)),
                'transaction_id'=>$profitSharingOrder['trans_id'],
                'out_order_no'=>$profitSharingOrder['profit_sharing_order_id'],
                'receivers'=>$profitSharingOrder['accounts'],
            ];
            $wxSign = new WxSign();
            $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->config['md5Key']);
            $postArr['sign'] = $sign;

            // 3.发送请求
            $curl = new CUrl(2);
            $url = 'https://api.mch.weixin.qq.com/secapi/pay/multiprofitsharing';
            $postXML = $wxSign->toXml($postArr);

            $opts = array(
                CURLOPT_HEADER    => 0,
                CURLOPT_SSL_VERIFYHOST    => false,
                CURLOPT_SSLCERTTYPE   => 'PEM', //默认支持的证书的类型，可以注释
                CURLOPT_SSLCERT   => $this->config['appCertPem'].'apiclient_cert.pem',
                CURLOPT_SSLKEY    => $this->config['appKeyPem'].'apiclient_key.pem',
            );
            Log::debug($opts);

            $curl_res = $curl->setOption($opts)->post($url,$postXML);

            $ret = $wxSign->toArray($curl_res);
            if($ret['return_code']=='SUCCESS' and $ret['result_code']=='SUCCESS'){
                //更新分账订单状态
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->status = 2;
                $profitShareModel->ps_time = date('Y-m-d H:i:s');
                $profitShareModel->ps_msg = json_encode($ret);
                $profitShareModel->save();
                $successCount++;

            }else{
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->ps_time = date('Y-m-d H:i:s');
                $profitShareModel->ps_msg = json_encode($ret);
                $profitShareModel->save();
                $failCount++;
            }
        }
        return [
            'code'=>0,
            'msg'=>'success',
            'data'=>[
                'processTime'=>date('Y-m-d H:i:s'),
                'totalCount'=>$totalCount,
                'successCount'=>$successCount,
                'failCount'=>$failCount
            ]
        ];
    }

    /**
     * 添加分账接收方
     *
     * @param $subAppId
     * @param $subMchId
     * @param $account
     * @return array|false
     * @throws Exception
     * @throws \Exception
     */
    public function addReceiver($subAppId, $subMchId, $account ,$is_mch=0)
    {
        // 1.生成签名
        $receiver = json_encode($account,256|64);
        $postArr = [
            'appid'=>$this->config['appId'],
            'mch_id'=>$this->config['mchId'],
//            'sub_mch_id'=>$subMchId,
//            'sub_appid'=>$subAppId,
            'nonce_str'=>md5(time() . rand(1000, 9999)),
            'receiver'=>$receiver
        ];
        if($is_mch == 1) {
            $postArr['sub_mch_id'] = $subMchId;
            $postArr['sub_appid'] =  $subAppId;
        }

        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
        $postArr['sign'] = $sign;
        Log::info($postArr);

        // 2.发送请求
        $curl = new CUrl();
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver';
        $postXML = $wxSign->toXml($postArr);

        // 3.处理结果
        $curl_res = $curl->post($url,$postXML);
        $ret = $wxSign->toArray($curl_res);
        return $ret;
    }

    /**
     * 删除分账接收方
     *
     * @param $subAppId
     * @param $subMchId
     * @param $account
     * @return array|false
     * @throws Exception
     * @throws \Exception
     */
    public function removeReceiver($subAppId, $subMchId, $account)
    {
        // 1.生成签名
        $receiver = json_encode($account,256|64);
        $postArr = [
            'appid'=>$this->config['appId'],
            'mch_id'=>$this->config['mchId'],
            'sub_mch_id'=>$subMchId,
            'sub_appid'=>$subAppId,
            'nonce_str'=>md5(time() . rand(1000, 9999)),
            'receiver'=>$receiver
        ];
        $wxSign = new WxSign();
        $sign = $wxSign->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
        $postArr['sign'] = $sign;

        // 2.发送请求
        $curl = new CUrl();
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingremovereceiver';
        $postXML = $wxSign->toXml($postArr);

        // 3.处理结果
        $curl_res = $curl->post($url,$postXML);
        $ret = $wxSign->toArray($curl_res);
        return $ret;
    }

    /**
     * 遍历分账订单列表，执行完结分账
     *
     * @return array
     * @throws Exception
     * @throws \Exception
     */
    public function finishAll()
    {
        $wxSign = new WxSign();
        $profitShareModel = new ProfitSharingList;

        $profitSharingOrders = $profitShareModel->all()->where(['status'=>2])->toArray();
        if(empty($profitSharingOrders)) return ['code'=>1, 'msg'=>'没有待分账订单'];

        $totalCount = count($profitSharingOrders);
        $successCount = 0;
        $failCount = 0;
        foreach ($profitSharingOrders as $profitSharingOrder)
        {
            // 当前导致分账失败的最多的问题是微信对同一张支付订单的分账频率限制
            // 因此，分账完结前，先判断该订单是否全部都已经完成分账，全部完成分账后再去调完结接口解冻资金
            if(!empty($profitShareModel->all()
                ->where('trans_id','=',$profitSharingOrder['trans_id'])
                ->where('status','=',2)
                ->toArray()))
            {
                $failCount++;
                continue;
            }
            $postArr = [
                'appid'=>$this->config['appId'],
                'mch_id'=>$this->config['mchId'],
                'sub_mch_id'=>$profitSharingOrder['sub_mch_id'],
                'transaction_id'=>$profitSharingOrder['trans_id'],
                'out_order_no'=>'finish_'.$profitSharingOrder['trans_id'],
                'description'=>'分账完结',
                'nonce_str'=>md5(time() . rand(1000, 9999)),
            ];
            $sign = $wxSign->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
            $postArr['sign'] = $sign;

            // 请求
            $curl = new CUrl(2);
            $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish';
            $postXML = $wxSign->toXml($postArr);

            $opts = array(
                CURLOPT_HEADER    => 0,
                CURLOPT_SSL_VERIFYHOST    => false,
                CURLOPT_SSLCERTTYPE   => 'PEM', //默认支持的证书的类型，可以注释
                CURLOPT_SSLCERT   => $this->config['appCertPem'].'apiclient_cert.pem',
                CURLOPT_SSLKEY    => $this->config['appKeyPem'].'apiclient_key.pem',
            );

            // 结果
            $curl_res = $curl->setOption($opts)->post($url,$postXML);
            $ret = $wxSign->toArray($curl_res);
            if($ret['return_code']=='SUCCESS' and $ret['result_code']=='SUCCESS'){
                //更新分账订单状态
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->status = 4;
                $profitShareModel->finish_time = date('Y-m-d H:i:s');
                $profitShareModel->finish_msg = json_encode($ret);
                $profitShareModel->save();
                $successCount++;

            }else{
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->finish_time = date('Y-m-d H:i:s');
                $profitShareModel->finish_msg = json_encode($ret);
                $profitShareModel->save();
                $failCount++;
            }
        }
        return [
            'code'=>0,
            'msg'=>'success',
            'data'=>[
                'processTime'=>date('Y-m-d H:i:s'),
                'totalCount'=>$totalCount,
                'successCount'=>$successCount,
                'failCount'=>$failCount
            ]
        ];
    }

    /**
     * 分账回退
     *
     * @param $channel
     * @param $transId
     * @param $orderId
     * @return array
     * @throws Exception
     * @throws \Exception
     */
    public function profitSharingReturn($orderId)
    {
        $return = array();
        $wxSign = new WxSign();

        $profitOrder = ProfitSharingList::firstWhere('profit_sharing_order_id','like','%'.$orderId.'%');
        if(empty($profitOrder))
            return ['code'=>1, 'data'=>[], 'msg'=>'未找到分账订单，请确认参数是否正确！'];
        
        log::info('查询的退款数据::'.json_encode($profitOrder,JSON_UNESCAPED_UNICODE));
        // 如果未分账，直接修改状态
        if($profitOrder->status==1)
        {
            $profitOrder->status = 3;
            $profitOrder->return_time = date('Y-m-d H:i:s');
            $profitOrder->return_msg = '在未分账的前提下发生退款';
            $profitOrder->save();
            return['code'=>0, 'data'=>[], 'msg'=>'回退成功'];
        }

        //if($profitOrder['state']==2){// 如果已经分账，等待分账完结，再执行分账回退
        //return ['code'=>1, 'data'=>[], 'msg'=>'订单已分账，请等待分账完结再执行退款'];
        //}

        if($profitOrder->status==3){// 如果已经回退，直接返回
            return['code'=>0, 'data'=>[], 'msg'=>'回退成功'];
        }

        $accounts = json_decode($profitOrder->accounts,true);
        foreach ($accounts as $account)
        {
            // 签名
            $postArr = [
                'appid'=>$this->config['appId'],
                'mch_id'=>$this->config['mchId'],
                'sub_mch_id'=>$profitOrder->sub_mchid,
                'sub_appid'=>$profitOrder->sub_appid,
                'nonce_str'=>md5(time() . rand(1000, 9999)),
                'out_order_no'=>$profitOrder->profit_sharing_order_id,
                'out_return_no'=>'return_'.$profitOrder->profit_sharing_order_id,
                'return_account_type'=>'MERCHANT_ID',
                'return_account'=>$account['account'],
                'return_amount'=>$account['amount'],
                'description'=>'用户退款',
                'sign_type'=>'HMAC-SHA256',
            ];
            $sign = $wxSign->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
            $postArr['sign'] = $sign;
            Log::info($postArr);
            log::info('证书路径::'.json_encode($this->config,JSON_UNESCAPED_UNICODE));

            // 请求
            $curl = new CUrl(2);
            $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharingreturn';
            $postXML = $wxSign->toXml($postArr);
            $opts = array(
                CURLOPT_HEADER    => 0,
                CURLOPT_SSL_VERIFYHOST    => false,
                CURLOPT_SSLCERTTYPE   => 'PEM', //默认支持的证书的类型，可以注释
                CURLOPT_SSLCERT   => $this->config['appCertPem'].'apiclient_cert.pem',
                CURLOPT_SSLKEY    => $this->config['appKeyPem'].'apiclient_key.pem',
            );

            // 结果
            $curl_res = $curl->setOption($opts)->post($url,$postXML);
            $ret = $wxSign->toArray($curl_res);
            log::info('退款结果::'.json_encode($ret,JSON_UNESCAPED_UNICODE));
            if($ret['return_code']=='SUCCESS' and $ret['result']=='SUCCESS'){
                //更新分账订单状态
                $profitOrder->status = 3;
                $profitOrder->return_time = date('Y-m-d H:i:s');
                $profitOrder->return_msg = json_encode($ret);
                $profitOrder->save();

            }else{
                $profitOrder->return_time = date('Y-m-d H:i:s');
                $profitOrder->return_msg = json_encode($ret);
                $profitOrder->save();
            }
            $return[] = $ret;
        }

        return ['code'=>0, 'data'=>$return, 'msg'=>'回退成功'];;
    }

    /**
     * 请求单次分账接口
     *
     * @return array
     * @throws Exception
     * @throws \Exception
     */
    public function profitSharing()
    {
        // 1.获取待分账订单
        $profitShareModel = new ProfitSharingList;
        $profitSharingOrders = $profitShareModel::where(['status'=>1])->orderby('id','desc')->get();

        $totalCount = count($profitSharingOrders);
        $successCount = 0;
        $failCount = 0;


        foreach ($profitSharingOrders as $profitSharingOrder)
        {
            
            $wxProfitSharingAccount = ProfitSharingAccount::where('sub_appid', $profitSharingOrder['sub_appid'])->where('sub_mchid', $profitSharingOrder['sub_mchid'])->first();
            
            if( isset($wxProfitSharingAccount) && $wxProfitSharingAccount->is_mch == 0) {
                
                // $account = array(
                //      'type' => 'MERCHANT_ID',
                //      'account' => '1586706251',
                //      'name' => '河北惠客优淘网络技术有限公司',
                //      'relation_type' => 'SERVICE_PROVIDER',
                //  );
                //  //添加收账人
                //  $rt = $this->addReceiver($profitSharingOrder['sub_appid'], $profitSharingOrder['sub_mchid'], $account ,  0);
                 
                //  log::info('添加收账人返回消息：：'.json_encode($rt,JSON_UNESCAPED_UNICODE));
                log::info('读取配置文件::'.$this->config['appCertPem']);
                
                //普通商户
                $postArr = array(
                    'mch_id'=>$profitSharingOrder['sub_mchid'],
                    'appid'=>$profitSharingOrder['sub_appid'],
                    'nonce_str'=>md5(time() . rand(1000, 9999)),
                    'transaction_id'=>$profitSharingOrder['trans_id'],
                    'out_order_no'=>$profitSharingOrder['profit_sharing_order_id'],
                    'receivers'=>$profitSharingOrder['accounts'],
                );
                
            } else {
                //服务商
                // 2.生成签名
                 $postArr = array(
                     'appid'=>$this->config['appId'],
                     'mch_id'=>$this->config['mchId'],
                     'sub_mch_id'=>$profitSharingOrder['sub_mchid'],
                     'sub_appid'=>$profitSharingOrder['sub_appid'],
                     'nonce_str'=>md5(time() . rand(1000, 9999)),
                     'transaction_id'=>$profitSharingOrder['trans_id'],
                     'out_order_no'=>$profitSharingOrder['profit_sharing_order_id'],
                     'receivers'=>$profitSharingOrder['accounts'],
                 );
            }

            $wxSign = new WxSign();
            $sign = $wxSign->getSign($postArr, 'HMAC-SHA256', $this->config['md5Key']);
            $postArr['sign'] = $sign;

            // 3.发送请求
            $curl = new CUrl(2);
            $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharing';
            $postXML = $wxSign->toXml($postArr);

            $opts = array(
                CURLOPT_HEADER    => 0,
                CURLOPT_SSL_VERIFYHOST    => false,
                CURLOPT_SSLCERTTYPE   => 'PEM', //默认支持的证书的类型，可以注释
                CURLOPT_SSLCERT   => $this->config['appCertPem'].'apiclient_cert.pem',
                CURLOPT_SSLKEY    => $this->config['appKeyPem'].'apiclient_key.pem',
            );

            // 4.结果
            $curl_res = $curl->setOption($opts)->post($url,$postXML);
            $ret = $wxSign->toArray($curl_res);
            log::info('分账结果::'.json_encode($ret,JSON_UNESCAPED_UNICODE));
            if($ret['return_code']=='SUCCESS' and $ret['result_code']=='SUCCESS'){
                //更新分账订单状态
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->status = 2;
                $profitShareModel->ps_time = date('Y-m-d H:i:s');
                $profitShareModel->ps_msg = json_encode($ret);
                $profitShareModel->save();
                $successCount++;

            }else{
                $profitShareModel = ProfitSharingList::find($profitSharingOrder['id']);
                $profitShareModel->ps_time = date('Y-m-d H:i:s');
                $profitShareModel->ps_msg = json_encode($ret);
                $profitShareModel->save();
                $failCount++;
            }
        }
        return array('processTime'=>date('Y-m-d H:i:s'), 'totalCount'=>$totalCount, 'successCount'=>$successCount, 'failCount'=>$failCount);
    }

    /**
     * 查询分账结果
     *
     * @return array|false
     * @throws \Exception
     */
    private function query()
    {
        $commonFunc = new CommonFunc();
        $wxPay = new WxSign();

        $profitOrder = $this->dataSource->getUniqueRow($this->channel, $this->transId, $this->orderTicket);
        if(empty($profitOrder)){
            return array('result_code'=>'FAIL', 'err_code_des'=>'未找到分账订单，请确认参数是否正确！');
        }

        // 1.生成签名
        $postArr = array(
            'mch_id'=>$this->config['mchId'],
            'sub_mch_id'=>$profitOrder['sub_mch_id'],
            'transaction_id'=>$this->transId,
            'out_order_no'=>$this->orderTicket,
            'nonce_str'=>md5(time() . rand(1000, 9999)),
        );
        $sign = $wxPay->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
        $postArr['sign'] = $sign;

        // 2.发送请求
        $curl = new CUrl();
        $url = 'https://api.mch.weixin.qq.com/pay/profitsharingquery';
        $postXML = $commonFunc->toXml($postArr);

        // 3.处理结果
        $curl_res = $curl->post($url,$postXML);
        $ret = $commonFunc->toArray($curl_res);
        return $ret;
    }

    /**
     * 完结分账
     *
     * @return array
     * @throws \Exception
     * @author xiewg
     */
    private function finish()
    {
        $commonFunc = new CommonFunc();
        $wxPay = new WxSign();

        // 签名
        $profitOrder = $this->dataSource->getUniqueRow($this->channel, $this->transId, $this->orderTicket);
        if(empty($profitOrder)){
            return array('result_code'=>'FAIL', 'err_code_des'=>'未找到分账订单，请确认参数是否正确！');
        }
        $postArr = array(
            'appid'=>$this->config['appId'],
            'mch_id'=>$this->config['mchId'],
            'sub_mch_id'=>$profitOrder['sub_mch_id'],
            'transaction_id'=>$this->transId,
            'out_order_no'=>'finish_'.$this->orderTicket,
            'description'=>!empty($this->description)?$this->description:'分账完结',
            'nonce_str'=>md5(time() . rand(1000, 9999)),
        );
        $sign = $wxPay->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
        $postArr['sign'] = $sign;

        // 请求
        $curl = new CUrl(2);
        $url = 'https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish';
        $postXML = $commonFunc->toXml($postArr);

        $opts = array(
            CURLOPT_HEADER    => 0,
            CURLOPT_SSL_VERIFYHOST    => false,
            CURLOPT_SSLCERTTYPE   => 'PEM', //默认支持的证书的类型，可以注释
            CURLOPT_SSLCERT   => $this->config['appCertPem'].'apiclient_cert.pem',
            CURLOPT_SSLKEY    => $this->config['appKeyPem'].'apiclient_key.pem',
        );

        // 结果
        $curl_res = $curl->setOption($opts)->post($url,$postXML);
        $ret = $commonFunc->toArray($curl_res);
        if($ret['return_code']=='SUCCESS' and $ret['result_code']=='SUCCESS'){
            //更新分账订单状态
            $params = array('trans_id'=>$profitOrder['trans_id']);
            $this->dataSource->updateProfitSharingOrder('ProfitSharingFinish', $params, json_encode($ret), 4);

        }else{
            $params = array('trans_id'=>$profitOrder['trans_id']);
            $this->dataSource->updateProfitSharingOrder('ProfitSharingFinish', $params, json_encode($ret));
        }
        return $ret;
    }

    /**
     * 回退结果查询
     *
     * @return array|false
     * @throws \Exception
     */
    private function returnQuery()
    {
        $ret = array();
        $commonFunc = new CommonFunc();
        $wxPay = new WxSign();

        $profitOrder = $this->dataSource->getUniqueRow($this->channel, $this->transId, $this->orderTicket);
        if(empty($profitOrder)){
            return array('result_code'=>'FAIL', 'err_code_des'=>'未找到分账订单，请确认参数是否正确！');
        }
        $accounts = json_decode($profitOrder['accounts'],true);
        foreach ($accounts as $account)
        {
            // 签名
            $postArr = array(
                'appid'=>$this->config['appId'],
                'mch_id'=>$this->config['mchId'],
                'sub_mch_id'=>$profitOrder['sub_mch_id'],
                'nonce_str'=>md5(time() . rand(1000, 9999)),
                'out_order_no'=>$profitOrder['order_ticket'],
                'out_return_no'=>'return_'.$profitOrder['order_ticket'].'_'.$account['account'],
                'sign_type'=>'HMAC-SHA256',
            );
            $sign = $wxPay->getSign($postArr, 'HMAC-SHA256',$this->config['md5Key']);
            $postArr['sign'] = $sign;

            // 请求
            $curl = new CUrl(2);
            $url = 'https://api.mch.weixin.qq.com/pay/profitsharingreturnquery';
            $postXML = $commonFunc->toXml($postArr);

            $curl_res = $curl->post($url,$postXML);

            $ret[] = $commonFunc->toArray($curl_res);
        }
        return $ret;
    }

    /**
     * 请求方获取分账订单记录
     *
     * @return array
     */
    private function getProfitSharingList()
    {
        $channel = $this->channel;
        $transId = $this->transId;
        $orderTicket = $this->orderTicket;
        $state   = $this->state;
        return $this->dataSource->getProfitSharingList($channel, $transId, $orderTicket, $state);
    }

}
